/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sport;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.ejb.*;
import javax.naming.NamingException;
import player.Player;
import player.PlayerRemote;
import team.Team;
import team.TeamRemote;

/**
 *
 * @author denaas
 */
public class PlayerManagerBean implements SessionBean {
    
    private SessionContext context;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification See section 7.11.3 of the
     * EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
    
    public void add(Player player) throws SQLException, NamingException, CreateException, RemoteException {
        PlayerHomeFabric.getInstance(player).create(player);
    }
    
    public void edit(Player player) throws SQLException, NamingException, FinderException, RemoteException {
        
        PlayerRemote remote = PlayerHomeFabric.getInstance(player).findByPrimaryKey(player.getId());
        
        List attrs = player.getAttrsNames();
        
        Player pl = remote.getPlayer();
        for (int i=0; i < attrs.size(); ++i) {
            String attr = (String)attrs.get(i);
            if (player.getAttrValue(attr) != null) {
                pl.setAttrValue(attr, player.getAttrValue(attr));
            }
        }
        
        remote.setPlayer(pl);
    }
    
    public void remove(Integer playerId, Integer sportId) throws SQLException, NamingException, FinderException, RemoteException, RemoveException {
        PlayerHomeFabric.getInstance(sportId).findByPrimaryKey(playerId).remove();
    }
    
    public List selectMany(Integer teamId, Integer sportId) throws SQLException, SQLNotFoundException, NamingException, RemoteException, FinderException {
        List result = new LinkedList();
           
        Vector players = new Vector(PlayerHomeFabric.getInstance(sportId).findByTeam(teamId));
        for(int i=0; i < players.size(); i++) {
            result.add(((PlayerRemote)players.get(i)).getPlayer());
        }
        return result;
    }
    
    public Player selectOne(Integer playerId, Integer sportId) throws SQLException, SQLNotFoundException, NamingException, RemoteException, FinderException {
        return PlayerHomeFabric.getInstance(sportId).findByPrimaryKey(playerId).getPlayer();
    }
            
    public List selectByQuery(Player plfrom, Player plto, Integer sportId) throws SQLException, SQLNotFoundException, NamingException, RemoteException {
        List result = new LinkedList();
        List remote = PlayerHomeFabric.getInstance(sportId).select(plfrom, plto);
        for(int i=0; i < remote.size(); i++) {
            result.add(remote.get(i));
        }
        return result;
    }
    
}
